app.run(["$templateCache", function($templateCache) {
  $templateCache.put("toast-confirm.html",
'<md-toast>\
    <span flex>{{text}}</span>\
    <md-button ng-click="cancel()" class="md-warn">Annuler</md-button>\
</md-toast>'
);
}]);

app.controller('confirmToastCtrl', function ($scope, $mdToast, text) {
    $scope.text = text;
    $scope.cancel = function () {
        $mdToast.hide("cancel");
    };
});

app.factory("$confirm", ["$mdToast", "$q", function ($mdToast, $q) {

    function ConfirmService() {
    }
    ConfirmService.prototype = {
        show: function (msg, hideDelay) {
            var deferred = $q.defer();
            $mdToast.show({
                controller: 'confirmToastCtrl',
                templateUrl: 'toast-confirm.html',
                hideDelay: hideDelay || 4500,
                position: "left top right",
                locals: {
                    text: msg
                }
            }).then(function (data) {
                deferred.reject("CANCEL");
            }, function (error) {
                deferred.resolve("OK");
            });
            return deferred.promise;
        },
        hide: function () {
            return $mdToast.hide();
        }
    }
    return new ConfirmService();
} ]);

